package com.pr.makfilm.service.impl;

import com.pr.makfilm.dao.IAbstractDao;
import com.pr.makfilm.dao.ISchoolTypeDao;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.SchoolType;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.ISchoolService;
import com.pr.makfilm.service.ISchoolTypeService;
import com.pr.makfilm.service.util.SidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 10.09.13
 * Time: 22:38
 * To change this template use File | Settings | File Templates.
 */
@Service(SchoolTypeServiceImpl.BEAN_NAME)
public class SchoolTypeServiceImpl extends AbstractServiceImpl<SchoolType> implements ISchoolTypeService
{
   public static final String BEAN_NAME = "schoolTypeService";

   @Resource
   private ISchoolTypeDao schoolTypeDao;

   @Resource
   private ISecurityService securityService;

   @Resource
   private ISchoolService schoolService;

   @Override
   protected IAbstractDao<SchoolType> getDao()
   {
      return schoolTypeDao;
   }

   @Override
   @Transactional
   public SchoolType save(SchoolType type)
   {
      type.setSid(SidUtils.generate());
      type.setObjectState(ObjectStateEnum.ACTIVE);
      type.setUser(securityService.getLoggedInUser());
      return super.save(type);
   }

   @Override
   @Transactional
   public List<SchoolType> findUserTypes()
   {
      return schoolTypeDao.findUserTypes(securityService.getLoggedInUser());
   }

   @Override
   @Transactional
   public SchoolType deactivate(SchoolType type)
   {
      SchoolType oldObj = getBySid(type.getSid());
      oldObj.setObjectState(ObjectStateEnum.DELETED);
      return update(oldObj);
   }
}
